package com.bitejiuyeke.job_publish.exception;

/**
 * @Author 比特就业课
 */

import com.bitejiuyeke.job_publish.common.AppResult;
import com.bitejiuyeke.job_publish.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 统一异常处理
 */
// 添加注解
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义的已知异常
     * @param e ApplicationException
     * @return AppResult
     */
    // 以body形式返回
    @ResponseBody
    // 指定要处理的异常
    @ExceptionHandler(ApplicationException.class)
    public AppResult handleApplicationException (ApplicationException e) {
        // 打印异常
        e.printStackTrace();
        // 记录日志
        log.error(e.getMessage());
        // 获取异常信息
        if (e.getErrorResult() != null) {
            // 返回异常类中记录的状态
            return e.getErrorResult();
        }
        // 默认返回异常信息
        return AppResult.failed(e.getMessage());
    }

    /**
     * 处理参数校验异常
     * @param e MethodArgumentNotValidException
     * @return AppResult
     */
    // 以body形式返回
    @ResponseBody
    // 指定要处理的异常
    @ExceptionHandler(BindException.class)
    public AppResult handleBindException (BindException e) {
        // 记录日志
        log.error(e.getMessage());
        // 解析异常信息
        String message = join(e.getAllErrors(),
                DefaultMessageSourceResolvable::getDefaultMessage, ", ");
        // 默认返回异常信息
        return AppResult.failed(message);
    }
    // 解析异常的方法
    private <E> String join(Collection<E> collection, Function<E, String>
            function, CharSequence delimiter) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }
        return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter));
    }

    /**
     * 处理全未捕获的其他异常
     * @param e Exception
     * @return AppResult
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult handleException (Exception e) {
        // 打印异常
        log.error(e.getMessage());
        // 记录日志
        log.error(e.getMessage());
        if (e.getMessage() == null) {
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        // 默认返回异常信息
        return AppResult.failed(e.getMessage());
    }

    /**
     * 参数校验异常
     * @param e HttpMessageNotReadableException
     * @return AppResult
     */
    @ResponseBody
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public AppResult handleHttpMessageNotReadableException (Exception e) {
        // 记录日志
        log.error(e.getMessage());
        if (e.getMessage() == null) {
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        // 默认返回异常信息
        return AppResult.failed("参数不能为空");
    }
}
